<template>
  <a-popover placement="bottom">
    <template slot="content">
      <a-button type="link" @click="openPassWordDialog">
        修改个人信息
      </a-button>
    </template>
    <div class="menu-item">
      <div class="user-info">
        <span class="user-avatar" v-text="userAvatar"></span>
        <span class="user-name" v-text="userInfo.nickName"></span>
      </div>
    </div>

    <a-modal
      title="修改个人信息"
      :visible="showPassWordDialog"
      :maskClosable="false"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <a-row :gutter="50">
        <a-col :span="24" v-if="!userInfo.superAdmin">
          <div class="form-input-material">
            <input
              class="form-control-material"
              placeholder=" "
              v-model="formData.nickName"
            />
            <label>昵称</label>
          </div>
        </a-col>
      </a-row>
      <a-row :gutter="50">
        <a-col :span="24">
          <div class="form-input-material">
            <input
              class="form-control-material"
              placeholder=" "
              v-model="formData.password"
            />
            <label>新密码</label>
          </div>
        </a-col>
      </a-row>
      <a-row :gutter="50" v-if="!userInfo.superAdmin">
        <a-col :span="24">
          <div class="form-input-material">
            <input
              class="form-control-material"
              placeholder=" "
              v-model="formData.confirmPassword"
            />
            <label>确认新密码</label>
          </div>
        </a-col>
      </a-row>
      <template slot="footer">
        <a-button
          key="submit"
          type="primary"
          :loading="loading"
          @click="handleOk"
        >
          修改
        </a-button>
        <a-button key="back" @click="handleCancel"> 取消 </a-button>
      </template>
    </a-modal>
  </a-popover>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
import { updateUserInfo, logout } from "@/api/user";
export default {
  data() {
    return {
      showPassWordDialog: false,
      formData: {},
      loading: false
    };
  },
  methods: {
    ...mapActions({
      logout: "global/logout",
      setList: "proto/setList"
    }),
    async userLogout() {
      try {
        const { code, msg } = await logout();
        if (code === 200) {
          await this.logout();
          this.setList([]);
          this.$router.push("/login");
        }
      } catch (error) {}
    },
    async handleOk() {
      const { nickName, password, confirmPassword } = this.formData;
      if (this.userInfo.superAdmin) {
        if (!password) {
          this.$notification.error({
            message: "请输入密码"
          });
          return;
        }
      } else {
        if (!nickName && !password) {
          this.$notification.error({
            message: "请输入您要修改的内容"
          });
          return;
        } else {
          if (password || confirmPassword) {
            if (!password) {
              this.$notification.error({
                message: "请输入密码"
              });
              return;
            }
            if (!confirmPassword) {
              this.$notification.error({
                message: "请输入确认密码"
              });
              return;
            }

            if (password !== confirmPassword) {
              this.$notification.error({
                message: "两次密码不相同，请重新输入"
              });
              return;
            }
          } else {
            if (!nickName) {
              this.$notification.error({
                message: "请输入昵称"
              });
              return;
            }
          }
        }
      }

      this.loading = true;
      try {
        const { code, msg } = await updateUserInfo(this.formData);
        if (code == 200) {
          this.handleCancel();
          this.userLogout();
          this.$notification.success({
            message: msg
          });
        }
      } catch (error) {}

      this.loading = false;
    },
    handleCancel() {
      this.showPassWordDialog = false;
      this.formData = {};
    },
    openPassWordDialog() {
      this.showPassWordDialog = true;
      this.formData = this.userInfo;
    }
  },
  computed: {
    ...mapGetters({
      userInfo: "userInfo"
    }),
    userAvatar() {
      const { nickName = "" } = this.userInfo;
      if (nickName) {
        return nickName[0].toLocaleUpperCase();
      } else {
        return nickName;
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.user-info {
  height: 100%;
  display: flex;
  align-items: center;
  .user-avatar {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    margin-right: 10px;
    text-align: center;
    line-height: 30px;
    font-size: 24px;
    background-color: #000;
  }
}

.form-input-material,
.form-check {
  margin: 20px 0 !important;
}
.form-check {
  label {
    margin-right: 10px;
  }
}
</style>
